package com.bo.atomicity;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Author: gpb
 * @Date: 2024/7/18 22:42
 * @Description:
 */
public class CASVersion {
    public static void main(String[] args) {
        AtomicStampedReference<String> reference = new AtomicStampedReference<>("A", 1);
        String value = reference.getReference();
        int stamp = reference.getStamp();
        System.out.println("v-1->value:" + value + ",version:" + stamp);
        boolean b = reference.compareAndSet(value, "B", stamp, ++stamp);
        System.out.println("v-2->yes or no : " + b + ",value:" + reference.getReference() + ",version:" + reference.getStamp());
        // 错误版本号示范
        boolean b1 = reference.compareAndSet("B", "C", 1, ++stamp);
        System.out.println("v-3->yes or no : " + b1 + ",value:" + reference.getReference() + ",version:" + reference.getStamp());
    }
}
